package com.cysyz.mylib.utils;

import com.cysyz.mylib.utils.myassert.MyAssert;
import com.cysyz.mylib.utils.stringconverter.StringConverter;
import com.cysyz.mylib.utils.stringconverter.impl.Str2DoubleConverter;
import com.cysyz.mylib.utils.stringconverter.impl.Str2IntConverter;
import com.cysyz.mylib.utils.xsvmaker.XsvMaker;
import com.cysyz.mylib.utils.xsvmaker.impl.XsvMakerImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyStringUtils extends StringUtils{
    private static List<StringConverter> stringConverters;

    static {
        stringConverters = new ArrayList<StringConverter>();
        stringConverters.add(new Str2IntConverter());
        stringConverters.add(new Str2DoubleConverter());
    }

    public static String firstChar2UpperCase(String str) {
        MyAssert.notNull(str);

        str  = str.substring(0,1).toUpperCase()+str.substring(1);

        return str;
    }

    public static String getSetter(String propertyName) {
        MyAssert.notNull(propertyName);

        return "set" + firstChar2UpperCase(propertyName);
    }
    public static String getGetter(String propertyName) {
        MyAssert.notNull(propertyName);

        return "get" + firstChar2UpperCase(propertyName);

    }

    public static String listToCsv(List<? extends Object> list) {
        MyAssert.Collections.notEmpty(list);

        Object element = list.get(0);
        if (element instanceof String || element instanceof Number) {
            XsvMaker xsvMaker = new XsvMakerImpl();
            for (Object ele : list) {
                xsvMaker.append(ele.toString());
            }
            return xsvMaker.getResult();
        } else {
            throw  new IllegalArgumentException("the element of collection must be String or Number");
        }
    }

    public static <T> List<T> csvToList(String csv, Class<T> valType) {
        StringConverter stringConverter = null;
        for (StringConverter _stringConverter : stringConverters) {
            if (_stringConverter.support(valType)) {
                stringConverter = _stringConverter;
            }
        }
        String[] split = csv.split(",");
        List<T> result = new ArrayList<T>();
        for (String str : split) {
            result.add((T) stringConverter.convert(str.trim()));
        }
        return result;
    }

    public static char lastChar(String str) {
        return str.charAt(str.length() - 1);
    }

    public static StringBuilder deleteLastChar(StringBuilder str) {
        return str.deleteCharAt(str.length() - 1);
    }

    /**
     * 去掉字符串中的HTML标记
     * @param s
     * @return
     */
    public static String getStringWithoutHTML(String s){
        int start=0;
        int i=0;
        while((start=s.indexOf("<!--[if"))!=-1){
            if(i>1000){
                break;
            }
            int end=s.indexOf("<![endif]-->");
            if(end!=-1){
                String comment=s.substring(start, end+12);
                s=s.replace(comment, "");
            }
            i++;
        }

        Pattern pat   =   Pattern.compile("\\s*<.*?>\\s*",
                Pattern.DOTALL   |   Pattern.MULTILINE   |
                        Pattern.CASE_INSENSITIVE);
        Matcher m   =   pat.matcher(s);
        String   rs   =   m.replaceAll("   ");
        rs   =   rs.replaceAll("&nbsp;",   "   ");
        rs   =   rs.replaceAll("&lt;",   "<");
        rs   =   rs.replaceAll("&gt;",   ">");
        return   rs;
    }









    /**
     * 字符串格式化工具
     */
    public static class Format {
        public static String toAbcd(String inputValue) {
            MyAssert.notNull(inputValue);
            StringBuilder returnValue = new StringBuilder();
            returnValue.append(inputValue.substring(0, 1).toUpperCase()).append(inputValue.substring(1).toLowerCase());
            return returnValue.toString();
        }

        public static String AB_AB2abAb(String inputValue) {
            MyAssert.notNull(inputValue);
            String[] words = inputValue.split("_");
            StringBuilder returnValue = new StringBuilder(words[0].toLowerCase());
            for (int i = 1 ; i < words.length ; i++) {
                returnValue.append(MyStringUtils.Format.toAbcd(words[i]));
            }
            return returnValue.toString();
        }

        public static String firstChar2Lower(String simpleName) {
            StringBuilder many1PropertyName = new StringBuilder();
            many1PropertyName.append(simpleName.substring(0, 1).toLowerCase());
            many1PropertyName.append(simpleName.substring(1));
            return many1PropertyName.toString();
        }
    }

}
